package base.reflection_demo.demo_07;

import base.reflection_demo.demo_05.UserDaoImpl;
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;

public class DynamicProxyTest {
    /**
     * 看一看Java动态代理给我们生成的动态代理类长什么样的.
     * 用反编译工具看下E:/MyUserDaoProxy.class
     *
     * 动态代理意义探究：
     * 动态代理是一种模式，当需要对已有逻辑进行加强的逻辑；当有回调的逻辑都可以
     */
    public static void main(String[] args) {
//        UserDao proxyInstance = (UserDao)Proxy.newProxyInstance(UserDaoImpl.class.getClassLoader(), UserDaoImpl.class.getInterfaces(),
//          new InvocationHandler() {
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                Object result = method.invoke(new UserDaoImpl(), args);
//                return result;
//            }
//        });
//        proxyInstance.register();

        byte[] classFile = ProxyGenerator.generateProxyClass("MyUserDaoProxy", UserDaoImpl.class.getInterfaces());
        String path = "E:/MyUserDaoProxy.class";
        try(FileOutputStream fos = new FileOutputStream(path)) {
            fos.write(classFile);
            fos.flush();
            System.out.println("代理类class文件写入成功");
        }catch (Exception e) {
            System.out.println("代理类class文件写入错误");
            e.printStackTrace();
        }

    }
}
